import axios from '@/utils/request'
import { getToken } from '@/utils/token'
export * from './channels'
// 登陆接口
export const loginAPI = (data) => {
  return axios({
    url: '/v1_0/authorizations',
    method: 'POST',
    data
  })
}

// 获取文章列表
export const getArticleList = (params) => {
  return axios({
    url: '/v1_0/articles',
    method: 'GET',
    params
  })
}

// 文章_不感兴趣 /v1_0/article/dislikes
export const dislikesAPI = (target) => {
  return axios({
    url: '/v1_0/article/dislikes',
    method: 'POST',
    data: {
      target
    }
  })
}

// 文章_举报
export const reportsAPI = ({ target, type, remark }) => {
  return axios({
    url: '/v1_0/article/reports',
    method: 'POST',
    data: {
      target,
      type,
      remark
    }

  })
}

/**
 * 获取文章详情
 * @param {*} id 文章id
 * @returns
 */
export const getArticleDetailAPI = (id) => {
  return axios({
    url: `/v1_0/articles/${id}`,
    method: 'GET'
  })
}

/**
 * 文章_关注作者
 * @param {*} target 作者ID
 * @returns
 */
export const followAuthorAPI = (target) => {
  return axios({
    url: '/v1_0/user/followings',
    method: 'POST',
    data: {
      target
    }
  })
}

/**
 * 文章_取消关注作者
 * @param {*} target 作者ID
 * @returns
 */
export const unFollowAuthorAPI = (target) => {
  return axios({
    url: `/v1_0/user/followings/${target}`,
    method: 'DELETE'
  })
}

/**
 * 对文章点赞
 * @param {*} target 文章ID
 * @returns
 */
export const likeArticleAPI = (target) => {
  return axios({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target
    }
  })
}

/**
 * 取消对文章点赞
 * @param {*} target 文章ID
 * @returns
 */
export const unLikeArticleAPI = (target) => {
  return axios({
    url: `/v1_0/article/likings/${target}`,
    method: 'DELETE'
  })
}

// 用户
// 用户_基本资料
export const getUser = () => {
  return axios({
    url: '/v1_0/user',
    method: 'GET'
  })
}

// 用户_个人资料
export const getUserProfile = () => {
  return axios({
    url: '/v1_0/user/profile',
    method: 'GET'
  })
}

// 用户_更新头像
export const updateImg = (formDate) => {
  return axios({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: formDate
  })
}

// 用户_更新资料
export const updataUser = ({ birthday, name }) => {
  return axios({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: {
      birthday,
      name
    }
  })
}

// 搜索
/**
 * 联想建议（自动补全）
 * @param {*} q 请求的前缀词句
 * @returns
 */
export const associativeSearch = (q) => {
  return axios({
    url: '/v1_0/suggestion',
    method: 'GET',
    params: {
      q
    }
  })
}

/**
 * 获取搜索结果
 * @param {*} params
 * @returns
 */
export const getSearchAPI = (params) => {
  return axios({
    url: '/v1_0/search',
    method: 'GET',
    params
  })
}

// 评论
/**
 * 评论 - 获取列表
 * @param {*} param0
 * @returns
 */
export const getCommentsAPI = ({ type, source, offset = null, limit = null }) => {
  return axios({
    url: '/v1_0/comments',
    method: 'GET',
    params: {
      type,
      source,
      offset,
      limit
    }
  })
}
/**
 * 评论 - 发布
 * @param {*} param0
 * @returns
 */
export const releaseAPI = ({ target, content, artId = null }) => {
  return axios({
    url: '/v1_0/comments',
    method: 'POST',
    data: {
      target,
      content,
      art_id: artId
    }
  })
}
/**
 * 评论 - 点赞
 * @param {*} target 评论ID
 * @returns
 */
export const likeAPI = (target) => {
  return axios({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target
    }
  })
}

/**
 * 评论 - 取消点赞
 * @param {*} target 评论ID
 * @returns
 */
export const dislikeAPI = (target) => {
  return axios({
    url: `/v1_0/comment/likings/${target}`,
    method: 'DELETE'
  })
}

export const refreshTokenAPI = () => axios({
  url: '/v1_0/authorizations',
  method: 'PUT',
  headers: {
    Authorization: `Bearer ${getToken('refresh_token')}`
  }
})
